package com.cdtu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtu.entity.Certify;
import com.cdtu.entity.CertifyType;
import com.cdtu.entity.RUserCertify;
import com.cdtu.mapper.CertifyMapper;
import com.cdtu.mapper.CertifyTypeMapper;
import com.cdtu.service.CertifyTypeService;
import com.cdtu.service.RUserCertifyService;
import com.cdtu.mapper.RUserCertifyMapper;
import com.cdtu.vo.CertifyDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
* @author Sakura
* @description 针对表【r_user_certify】的数据库操作Service实现
* @createDate 2023-01-13 16:27:40
*/
@Service
public class RUserCertifyServiceImpl extends ServiceImpl<RUserCertifyMapper, RUserCertify>
    implements RUserCertifyService{

    @Autowired
    private RUserCertifyMapper rUserCertifyMapper;

    @Autowired
    private CertifyTypeService certifyTypeService;
    @Autowired
    private CertifyTypeMapper certifyTypeMapper;
    @Autowired
    private CertifyMapper certifyMapper;

    @Override
    public List<Long> findByUserId(long user_id) {
        return rUserCertifyMapper.findByUserId(user_id);
    }

    @Override
    public boolean addUserCertify(List<CertifyDTO> rUserCertifyList, long uid, long aid) {

        for (CertifyDTO dto : rUserCertifyList) {
            //添加肯定等于0，直接添加
            RUserCertify rUserCertify=new RUserCertify();
            rUserCertify.setUserId(uid);
            rUserCertify.setCheckUser(aid);

            String cname = dto.getName();
            String cType = dto.getType();

            CertifyType certifyType = certifyTypeMapper.findAllByName(cType);
            long tid=System.currentTimeMillis();
            long cid= System.currentTimeMillis();

            if (certifyType==null){//证书类型都为空的话肯定添加类型和证书
                certifyType=new CertifyType();
                certifyType.setId(tid);
                certifyType.setName(cType);
                boolean result = certifyTypeService.save(certifyType);
                if (!result){
                    return false;
                }
//                存入证书类型后需要存入证书
                Certify certify=new Certify();
                certify.setId(cid);
                certify.setName(cname);
                certify.setTypeId(tid);
                int insert = certifyMapper.insert(certify);
                if (insert<1){
                    return false;
                }

                //添加证书和人的关系
                rUserCertify.setCertifyId(cid);
                rUserCertify.setImageSrc(dto.getImageUrl());

                //证书时间为三年
//                Date date = new Date();
//                Calendar cal = Calendar.getInstance();
//                cal.setTime(date);//设置起时间
//                cal.add(Calendar.YEAR, 3);//三年之后失效
//                rUserCertify.setDeadline(cal.getTime());
                rUserCertify.setDeadline(dto.getDeadline());
                insert = rUserCertifyMapper.insert(rUserCertify);

//            每一次都要判断
                if (insert<1){
                    return false;
                }

            }else {
                Certify certify = certifyMapper.selectByName(cname);

                if (certify==null){
                    //证书类型存在，证书不存在
//                说明数据库没有收录该证书，我们就需要添加该证书
                    certify=new Certify();
                    certify.setId(cid);
                    certify.setName(cname);
                    certify.setTypeId(certifyType.getId());
                    int result = certifyMapper.insert(certify);
                    if (result==0){
                        return false;
                    }
                    //证书添加成功之后需要添加关系
                    rUserCertify.setCertifyId(cid);
                    rUserCertify.setImageSrc(dto.getImageUrl());

                    //证书时间为三年
//                    Date date = new Date();
//                    Calendar cal = Calendar.getInstance();
//                    cal.setTime(date);//设置起时间
//                    cal.add(Calendar.YEAR, 3);//三年之后失效
//                    rUserCertify.setDeadline(cal.getTime());
                    rUserCertify.setDeadline(dto.getDeadline());

                    result = rUserCertifyMapper.insert(rUserCertify);

//            每一次都要判断
                    if (result<1){
                        return false;
                    }

                }else {
                    //证书也存在，直接添加
                    rUserCertify.setCertifyId(certify.getId());

                    rUserCertify.setImageSrc(dto.getImageUrl());

                    //证书时间为三年
//                    Date date = new Date();
//                    Calendar cal = Calendar.getInstance();
//                    cal.setTime(date);//设置起时间
//                    cal.add(Calendar.YEAR, 3);//三年之后失效
//                    rUserCertify.setDeadline(cal.getTime());
                    rUserCertify.setDeadline(dto.getDeadline());

                    int result = rUserCertifyMapper.insert(rUserCertify);

//            每一次都要判断
                    if (result<1){
                        return false;
                    }
                }

            }

        }
        //都没有问题就返回true
        return true;
    }

    @Override
    public boolean removeByUid(long uid) {
        return rUserCertifyMapper.removeByUid(uid);
    }
}




